import { createSlice } from '@reduxjs/toolkit';
import type { PayloadAction } from '@reduxjs/toolkit';

interface CommonState {
  loading: boolean;
  collapsed: boolean;
  theme: 'light' | 'dark';
  userInfo: {
    username: string;
    role: string;
  } | null;
}

const initialState: CommonState = {
  loading: false,
  collapsed: false,
  theme: 'light',
  userInfo: null,
};

const commonSlice = createSlice({
  name: 'common',
  initialState,
  reducers: {
    setLoading: (state, action: PayloadAction<boolean>) => {
      state.loading = action.payload;
    },
    setCollapsed: (state, action: PayloadAction<boolean>) => {
      state.collapsed = action.payload;
    },
    setTheme: (state, action: PayloadAction<'light' | 'dark'>) => {
      state.theme = action.payload;
    },
    setUserInfo: (state, action: PayloadAction<CommonState['userInfo']>) => {
      state.userInfo = action.payload;
    },
  },
});

export const {
  setLoading,
  setCollapsed,
  setTheme,
  setUserInfo,
} = commonSlice.actions;

export default commonSlice.reducer;